package UTGBusiness;

import ExchangeBusiness.TradeManager;
import XMLForms.OTEM;
import XMLForms.TCM;
import XMLForms.TTCM;
import XMLForms.TTRM;
import XMLForms.TVFM;
import XMLForms.WTRM;
import XMLTranslator.XMLWriter;
import java.util.Date;

/**
 *
 * @author yuxia
 */
public class UTG {

    private IDDManager idman;
    private TDManager tdman;
    private Validator v;
    private XMLWriter writer;

    public UTG() {
        idman = new IDDManager();
        tdman = new TDManager();
        v = new Validator(idman, tdman);
    }

    public Object execute(TTRM request) {

        //record client message
        writer = new XMLWriter(request);
        writer.composeTTRM();
        //test client message and do things
        String test = "";
        test = checkClientMsg(request);
        if (!test.equals("")) {
            //test failre, send back message
            TVFM tv = createTVRM(request, test.split(":")[0],
                    test.split(":")[1]);
            writer.composeTVFM(test.split(":")[0], test.split(":")[1]);
            return tv;
        } else {
            //if test success, create WTRM and send to exchange
            WTRM wtrm = createWTRM(request, idman);
//            writer.composeWTRM(wtrm.getFirmID());

            //add this transaction into database
            tdman.addDBTR(request, idman.getFirmID(request.getStockID()),
                    idman.getFirmName(request.getStockID()));
            TCM response = new TradeManager().execute(wtrm);
            //test exchange message
            test = checkExchangeMsg(response);
            if (!test.equals("")) {

                TVFM tv = createTVRM(request, test.split(":")[0],
                        test.split(":")[1]);
                writer.composeTVFM(test.split(":")[0], test.split(":")[1]);
                if (test.equals("DBTR:DBTRNotFound")) {
                    tdman.addDBTR(response, idman.getFirmID(request.getStockID()),
                            idman.getFirmName(request.getStockID()));

                }
                tdman.updateDBTR(response.getFirmSequencNumber(), test);
                tdman.updateModificationTime(response.getFirmSequencNumber());
                return tv;
            } else {
                //test past create TTCM and send to Trader
                tdman.updateDBTR(response.getFirmSequencNumber(), "Trade Complete");
                System.out.println(response.getFirmSequencNumber() + " " + response.getAcceptorID());
                tdman.updateAcceptorID(response.getFirmSequencNumber(), response.getAcceptorID());
                tdman.updateModificationTime(response.getFirmSequencNumber());
                return createTTCM(response);
            }
        }
    }

    private String checkClientMsg(TTRM request) {
        String result = "";

        /*
        if (checkOfferIDValid(request.getOffererID())){
        TVRM tvrm = new TVRM(request, "OffererIDnotfound");
        return tvrm;
        }
         */

        if (!v.checkTraderIDValid(request.getTraderID())) {
            result = "TraderID:TraderIDnotfound";
        } else if (!v.checkShareCount(request.getShareCount())) {
            result = "ShareCount:ShareCountNotValid";
        } else if (!v.checkTimeValid(request.getOfferTime())) {
            result = "Time:TimeFormatError";
        }
        return result;
    }

    private String checkExchangeMsg(TCM response) {
        String result = "";

        if (v.checkDBTRNotFound(response.getFirmSequencNumber())) {
            result = "DBTR:DBTRNotFound";
        }

        if (!v.checkCompletionTimeValid(response.getCompletionTime())) {
            result = "CompletionTime:CompletionTimeError";
        }

        if (!v.checkTraderIDValid(response.getAcceptorID())) {
            result = "AccepterID:AccepterIDnotfound";
        }

        if (!v.checkPricePerShareValid(response.getTtrm().getSharePrice())) {
            result = "PricePerShare:PricePerShareNotValid";
        }

        if (v.checkTimeExceed(response.getFirmSequencNumber())) {
            result = "Time:TimeExceeded";
        }

        return result;
    }

    private WTRM createWTRM(TTRM req, IDDManager man) {
        String firmID = man.getFirmID(req.getStockID());
        String firmName = man.getFirmName(req.getStockID());
        String sequence;
        sequence = firmID + "-" + firmName + "-" + req.getTraderTimeSentStamp();
        return new WTRM(req, firmID, sequence);
    }

    private TVFM createTVRM(TTRM req, String field, String error) {
        return new TVFM(req, field, error);
    }

    private TTCM createTTCM(TCM res) {
        return new TTCM(res.getTtrm(), res.getAcceptorID(),
                res.getFirmSequencNumber(), res.getTradeState(),
                res.getCompletionTime());
    }

    private OTEM createOTEM(int tradeID) {
        return null;
    }
}
